package com.lht.femalecat.netty.stick;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @Author:骆宏图
 * @CreateDate: 14:16 2018/7/27.
 * @Description:
 * @Modified By:
 */
public class TimeClient {

    public static int count;

    public static void main(String[] args){

        EventLoopGroup group=new NioEventLoopGroup();

        Bootstrap b=new Bootstrap();


        try {
            b.group(group);
            b.channel(NioSocketChannel.class);
//            b.option(ChannelOption.SO_BACKLOG,1024);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
//                    socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
//                    socketChannel.pipeline().addLast(new StringDecoder());
                    socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter(){


                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//                            super.exceptionCaught(ctx, cause);
                            System.out.println("Unexpected exception form downstream:"+cause.getMessage());
                            ctx.close();
                        }

                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
//                            super.channelActive(ctx);
                            String str="Query Time Order"+System.getProperty("line.separator");
                            byte[] req=str.getBytes();
//                            ByteBuf first= Unpooled.copiedBuffer(req);
//                            ctx.writeAndFlush(first);
                            ByteBuf message=null;
                            for(int i=0;i<100;i++){
                                message= Unpooled.buffer(req.length);
                                message.writeBytes(req);
                                ctx.writeAndFlush(message);
                            }
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            //super.channelRead(ctx, msg);
                            ByteBuf buf=(ByteBuf)msg;
                            byte[] req=new byte[buf.readableBytes()];
                            buf.readBytes(req);
                            String body=new String(req,"UTF-8");
                            System.out.println("Now is ："+body+",this counter is :"+ ++TimeClient.count);
                        }
                    });
                }


            });
            //发起同步连接操作
            ChannelFuture f=b.connect(new InetSocketAddress("127.0.0.1",8080)).sync();
            //等待客户端链路关闭
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }

    }

}
